# encoding: utf-8
from pydantic import BaseModel


class Response(object):
    def __init__(self):
        self.code = 0
        self.message = "success"
        self.data = None
        self.details = None

    def serialize_to_dict(self):
        result = {'code': self.code,
                  'message': self.message,
                  'details': self.details, }
        if hasattr(self, 'data') and self.data is not None:
            if isinstance(self.data, list):
                for index, value in enumerate(self.data):
                    if isinstance(value, BaseModel):
                        self.data[index] = value.dict(exclude_none=True)
            elif isinstance(self.data, dict):
                for key, value in self.data.items():
                    if isinstance(value, list):
                        self.data[key] = []
                        for index, value2 in enumerate(value):
                            if isinstance(value2, BaseModel):
                                self.data[key].append(value2.dict(exclude_none=True))
                            else:
                                self.data[key].append(value2)
                    elif isinstance(value, BaseModel):
                        self.data[key] = value.dict(exclude_none=True)
                    else:
                        self.data[key] = value
            elif isinstance(self.data, BaseModel):
                self.data = self.data.dict(exclude_none=True)
            result['data'] = self.data
        if hasattr(self, 'total'):
            result['total'] = self.total
        return result


class SuccessResponse(Response):
    def __init__(self, data=None):
        super(SuccessResponse, self).__init__()
        self.code = 0
        self.message = "success"
        self.data = data


class ExceptionResponse(Response):
    def __init__(self, message=''):
        super(ExceptionResponse, self).__init__()
        self.code = -1
        self.message = "异常错误:" + message


class ParamsCheckErrorResponse(Response):
    def __init__(self, message=''):
        super(ParamsCheckErrorResponse, self).__init__()
        self.code = 1001
        self.message = "请检查请求入参:" + message


class UnknownBizCodeResponse(Response):
    def __init__(self, message=''):
        super(UnknownBizCodeResponse, self).__init__()
        self.code = 1002
        self.message = "未知的bizCode:" + message


class UnknownFoundHandleResponse(Response):
    def __init__(self, message=''):
        super(UnknownFoundHandleResponse, self).__init__()
        self.code = 1003
        self.message = "未找到处理函数:" + message


class TimeoutErrorResponse(Response):
    def __init__(self, message=''):
        super(TimeoutErrorResponse, self).__init__()
        self.code = 1004
        self.message = "接口处理超时:" + message


class GetDataFailResponse(Response):
    def __init__(self, message=''):
        super(GetDataFailResponse, self).__init__()
        self.code = 1005
        self.message = "从his获取数据失败:" + message


class UnFoundAppConfigResponse(Response):
    def __init__(self, message=''):
        super(UnFoundAppConfigResponse, self).__init__()
        self.code = 1006
        self.message = "未查询到机构配置信息:" + message


class SyncDepartmentFailResponse(Response):
    def __init__(self, message=''):
        super(SyncDepartmentFailResponse, self).__init__()
        self.code = 1007
        self.message = "同步科室信息失败:" + message


class SyncPersonFailResponse(Response):
    def __init__(self, message=''):
        super(SyncPersonFailResponse, self).__init__()
        self.code = 1008
        self.message = "同步人员信息失败:" + message


class SyncMedicineFailResponse(Response):
    def __init__(self, message=''):
        super(SyncMedicineFailResponse, self).__init__()
        self.code = 1009
        self.message = "同步药品信息失败:" + message


class SyncAdministratorRouteFailResponse(Response):
    def __init__(self, message=''):
        super(SyncAdministratorRouteFailResponse, self).__init__()
        self.code = 1010
        self.message = "同步给药途径信息失败:" + message


class SyncFreqFailResponse(Response):
    def __init__(self, message=''):
        super(SyncFreqFailResponse, self).__init__()
        self.code = 1011
        self.message = "同步给药频次信息失败:" + message


class SyncDiseaseCodeFailResponse(Response):
    def __init__(self, message=''):
        super(SyncDiseaseCodeFailResponse, self).__init__()
        self.code = 1012
        self.message = "同步疾病编码信息失败:" + message
